﻿using System;
using System.Collections.Generic;
using IsolationLevel = System.Transactions.IsolationLevel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BDShop.DAL;
using BDShop.Entities;
using System.Data;

namespace BDShop.Business
{
    public class WishListService
    {
        public static bool Insert(WishList wl)
        {
            try
            {
                using (var context = new BDShopEntities(IsolationLevel.ReadCommitted))
                {
                    if (context.WishLists.Where(w => w.CustomerID == wl.CustomerID && w.ProductID == wl.ProductID).ToList().Count == 0)
                    {
                        context.WishLists.Add(wl);
                        context.SaveChanges();
                    }
                    return true;

                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool Remove(WishList wl)
        {
            try
            {
                using (var context = new BDShopEntities(IsolationLevel.ReadCommitted))
                {
                    context.WishLists.Attach(wl);
                    context.WishLists.Remove(wl);
                    context.Entry(wl).State=EntityState.Deleted;
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static List<WishList> GetByUserID(int userID)
        {
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                return context.WishLists.Where(w => w.CustomerID == userID).OrderByDescending(w => w.DateCreated).ToList();
            }
        }

        public static WishList GetByID(long ID)
        {
            using (var context = new BDShopEntities(IsolationLevel.ReadUncommitted))
            {
                return context.WishLists.Where(w => w.ID == ID).First();
            }
        }
    }
}
